var handle,
  weeks_ch = ["日", "一", "二", "三", "四", "五", "六"],
  dateType = "yyyy|y|mm|m|dd|d|hh|h|ii|i|ss|s",
  _ = require("../lib/underscore.js")

handle = {
  /**
   * 时间戳转换为对象 (new Date()) => (timeObject)
   * @param  {Number}   time   传入原时间
   * @return {Object}   object 转换后的时间戳，和时间对象
   */
  timeToDateObj: function(time = new Date().getTime()) {
    var date = new Date()
    date.setTime(time)

    return {
      time: time,
      day: date.getDate(),
      month: date.getMonth() + 1,
      year: date.getFullYear(),
      week: weeks_ch[date.getDay()],
      hours: date.getHours(),
      minutes: date.getMinutes(),
      seconds: date.getSeconds(),
      monthStr: this.digit(date.getMonth() + 1),
      dayStr: this.digit(date.getDate()),
      hoursStr: this.digit(date.getHours()),
      minutesStr: this.digit(date.getMinutes()),
      secondsStr: this.digit(date.getSeconds())
    }
  },

  /**
   * 格式化时间戳
   * @param  {Number}    time           需要转换的时间戳。默认当前时间
   * @param  {String}    format         时间格式 默认 yyyy-mm-dd HH:ii:ss
   * @return {String}    timeStr        格式化后的字符串
   */
  formateTime: function(time = new Date().getTime(), format = "yyyy-mm-dd hh:ii:ss") {
    // 转换时间对象
    var timeObj = this.timeToDateObj(time)
    // 正则
    format = format.toLocaleLowerCase().match(new RegExp(dateType + "|.", "g")) || []
    // 匹配格式
    _.each(format, (v, i) => {
      if (/yyyy|y/.test(v)) {
        //年
        format[i] = this.digit(timeObj.year, v.length)
      } else if (/mm|m/.test(v)) {
        //月
        format[i] = this.digit(timeObj.month, v.length)
      } else if (/dd|d/.test(v)) {
        //日
        format[i] = this.digit(timeObj.day, v.length)
      } else if (/hh|h/.test(v)) {
        //时
        format[i] = this.digit(timeObj.hours, v.length)
      } else if (/ii|i/.test(v)) {
        //分
        format[i] = this.digit(timeObj.minutes, v.length)
      } else if (/ss|s/.test(v)) {
        //秒
        format[i] = this.digit(timeObj.seconds, v.length)
      }
    })
    return format.join("")
  },

  /**
   * 倒计时
   * @param {Number} startTime 开始时间
   * @param {Number} endTime 结束时间
   */
  getTimeCounter: function(startTime = new Date().getTime(), endTime = new Date().getTime()) {
    var leftTime = endTime - startTime
    var leftTimeStr = ""
    var timeObj = {}
    if (leftTime > 0) {
      leftTimeStr = "剩余 "
      timeObj = {
        day: this.digit(Math.floor(leftTime / 1000 / 60 / 60 / 24)),
        hoursStr: (Math.floor((leftTime / 1000 / 60 / 60) % 24) / 100).toFixed(2).split(".")[1],
        minutesStr: (Math.floor((leftTime / 1000 / 60) % 60) / 100).toFixed(2).split(".")[1],
        secondsStr: (Math.floor((leftTime / 1000) % 60) / 100).toFixed(2).split(".")[1],
        end: false
      }
      if (timeObj.day / 1 > 0) {
        leftTimeStr = leftTimeStr + timeObj.day + "天"
      }
      timeObj.timeStr = leftTimeStr + timeObj.hoursStr + ":" + timeObj.minutesStr + ":" + timeObj.secondsStr
    } else {
      timeObj = {
        day: 0,
        hoursStr: 0,
        minutesStr: 0,
        secondsStr: 0,
        end: true,
        timeStr: "已结束"
      }
    }

    return timeObj
  },
  /**
   * 数字前置补零
   * @param  {Number}  num     传入的数字
   * @param  {Number}  length  需要的长度。默认2位
   * @return {Number}  num     转换好的数字
   */
  digit: function({ num, length = 2 }) {
    var str = ""
    num = String(num)
    length = length || 2
    for (var i = num.length; i < length; i++) {
      str += "0"
    }
    return num < Math.pow(10, length) ? str + (num | 0) : num
  }
}

module.exports = handle
